<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:schoolPlan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:schoolPlan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:schoolPlan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:schoolPlan:export']"
        >导出</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
                        <el-form-item label="年份" prop="year" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.year"
                                    placeholder="请输入年份"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="学校代码" prop="schoolCode" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.schoolCode"
                                    placeholder="请输入学校代码"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="学校名称" prop="schoolName" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.schoolName"
                                    placeholder="请输入学校名称"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        
                        <el-form-item label="专业代码" prop="majorCode" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.majorCode"
                                    placeholder="请输入专业代码"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="专业名称" prop="majorName" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.majorName"
                                    placeholder="请输入专业名称"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>                        
            <el-form-item class="el-form-search-item">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="schoolPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="#" type="index" width="50" align="center">
            <template scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
            </template>
        </el-table-column>
      
      <el-table-column label="年份" align="center" prop="year" />
	  <el-table-column label="批次" align="center" prop="batch" />
      <el-table-column label="学校代码" align="center" prop="schoolCode" />
      <el-table-column label="学校名称" align="center" prop="schoolName" />
      <el-table-column label="学校招生人数" align="center" prop="schoolPlan" />
	  <el-table-column label="理工类招生人数" align="center" prop="majorLgPlan" />
	  <el-table-column label="文史类招生人数" align="center" prop="majorWsPlan" />
      <el-table-column label="专业代码" align="center" prop="majorCode" />
      <el-table-column label="专业名称" align="center" prop="majorName" />  
	  <el-table-column label="专业顺序" align="center" prop="majorSeq" />	  
      <el-table-column label="专业招生人数" align="center" prop="majorPlan" />
      <el-table-column label="专业分类" align="center" prop="majorType" />
      <el-table-column label="专业组" align="center" prop="zyz" />
      <el-table-column label="首选科目" align="center" prop="km1" />
      <el-table-column label="再选科目" align="center" prop="km2" />
	  <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:schoolPlan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:schoolPlan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学校招生计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <div class="form-item-row">
		<el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" style="width: 215px;"/>
        </el-form-item>
        <el-form-item label="批次" prop="batch" label-width="100px">
          <el-input v-model="form.batch" placeholder="请输入批次" style="width: 215px;"/>
        </el-form-item>
		</div>
		<div class="form-item-row">
		<el-form-item label="学校代码" prop="schoolCode" label-width="100px">
		  <el-input v-model="form.schoolCode" placeholder="请输入学校代码" style="width: 215px;"/>
		</el-form-item>
        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="form.schoolName" placeholder="请输入学校名称" style="width: 215px;"/>
        </el-form-item>
        
		</div>
		<div class="form-item-row">
		<el-form-item label="学校招生人数" prop="schoolPlan" label-width="100px">
		  <el-input v-model="form.schoolPlan" placeholder="请输入学校计划人数" style="width: 215px;"/>
		</el-form-item>
        <el-form-item label="专业代码" prop="majorCode">
          <el-input v-model="form.majorCode" placeholder="请输入专业代码" style="width: 215px;" />
        </el-form-item>
        
		</div>
		<div class="form-item-row">
		<el-form-item label="专业名称" prop="majorName" label-width="100px">
		  <el-input v-model="form.majorName" placeholder="请输入专业名称" style="width: 215px;"/>
		</el-form-item>
        <el-form-item label="专业顺序" prop="majorSeq">
          <el-input v-model="form.majorSeq" placeholder="请输入专业顺序" style="width: 215px;"/>
        </el-form-item>
        
		</div>
		<div class="form-item-row">
			<el-form-item label="专业招生人数" prop="majorPlan" label-width="100px">
			  <el-input v-model="form.majorPlan" placeholder="请输入专业招生人数" style="width: 215px;"/>
			</el-form-item>
		  <el-form-item label="专业组" prop="zyz">
		    <el-input v-model="form.zyz" placeholder="请输入专业组" style="width: 215px;"/>
		  </el-form-item>
		</div>
    <div class="form-item-row">
      <el-form-item label="首选考科目" prop="km1">
        <el-select v-model="form.km1" placeholder="请选择科目" style="width: 100%" >
            <el-option
              v-for="dict in dict.type.fir_sel_pro"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="再选考科目" prop="km2" label-width="100px">
          <el-select v-model="form.km2" placeholder="请选择科目" style="width: 100%" multiple @change="km2Change" :multiple-limit="2">
            <el-option
              v-for="dict in dict.type.sec_sel_pro"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              >
            </el-option>
          </el-select>
        </el-form-item>
    </div>
    
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchoolPlan, getSchoolPlan, delSchoolPlan, addSchoolPlan, updateSchoolPlan } from "@/api/system/schoolPlanZk";

export default {
  name: "SchoolPlanZk",
  dicts: ['fir_sel_pro','sec_sel_pro'],
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学校招生计划表格数据
      schoolPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "",
        isAsc: "",
        year: null,
		batch: null,
        schoolCode: null,
        schoolName: null,
        schoolPlan: null,
        majorCode: null,
        majorName: null,
        majorSeq: null,
        majorPlan: null,
        majorType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tempKm2:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    km2Change(list){
      let selectVal = list.filter(item => this.tempKm2.indexOf(item) == - 1)
      if(selectVal.indexOf('不限') > -1){
        this.$set(this.form,'km2',['不限'])
        list = ['不限']
      }else{
        if(this.tempKm2.indexOf('不限') > -1){
          this.$set(this.form,'km2',selectVal)
          list = selectVal
        }
      }
      this.tempKm2 = list
      return
    },
    handleExport() {},
    /** 查询学校招生计划列表 */
    getList() {
      this.loading = true;
      listSchoolPlan(this.queryParams).then(response => {
        this.schoolPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        year: null,
		batch: null,
        schoolCode: null,
        schoolName: null,
        schoolPlan: null,
        majorCode: null,
        majorName: null,
        majorSeq: null,
        majorPlan: null,
        majorType: null,
        km1:null,
        km2:null,
        zyz:null
      };
      this.tempKm2 = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学校招生计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchoolPlan(id).then(response => {
        let data = {
          ...response.data
        }
        if(response.data.hasOwnProperty("km2")){
          data.km2 = response.data.km2 != null ? response.data.km2.split(',') : []
          this.tempKm2 = data.km2
        }
        this.form = JSON.parse(JSON.stringify(data));
        this.open = true;
        this.title = "修改学校招生计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //重排
          let params = {
            ...this.form,
            km2:this.form.km2.join(",")
          }
          if (this.form.id != null) {
            updateSchoolPlan(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchoolPlan(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delSchoolPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
<style>

.form-item-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.el-form-item__content {
  padding-top: 5px; /* 减小垂直间隔 */
  padding-bottom: 5px; /* 减小垂直间隔 */
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

</style>
